package Lee_code;


import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author blh
 * @date 2024年06月24日 9:44
 */
public class Lee_102_624 {
    List<List<Integer>> res = new LinkedList<>();

    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root==null){
            return res;
        }
        Deque<TreeNode> deque = new LinkedList<TreeNode>();
        deque.offer(root);
        while (!deque.isEmpty()) {
            int size = deque.size();
            List<Integer> list = new LinkedList<>();
            for (int i = 0; i < size; i++) {
                TreeNode now = deque.poll();
                if (now != null) {
                    list.add(now.val);
                    if (now.left != null) {
                        deque.offer(now.left);
                    }
                    if (now.right != null) {
                        deque.offer(now.right);
                    }
                }
            }
            res.add(list);
        }
        return res;
    }
}
